<?php
namespace Course\Controller;
use Common\Controller\AdminbaseController;
require('./Expand/cos/include.php');
use Qcloud_cos\Auth;
use Qcloud_cos\Cosapi;
class AdminSection2Controller extends AdminbaseController {
	protected $section_obj;
    protected $section_obj2;
	protected $course_obj;

	function _initialize() {
		parent::_initialize();
		$this->section_obj = D("Common/Section");
        $this->section_obj2 = D("Common/Section2");
		$this->sectionview_obj = D("Common/SectionView");
        $this->sectionview_obj2 = D("Common/SectionView2");
		$this->course_obj = D("Common/Course");
	}


	function index(){
		$this->_lists();
		//$this->_getCourse();
		$this->display();
	}

	function add(){
		$cs_id=  intval(I("get.cs_id"));
		$cs_data=$this->section_obj->where("id=$cs_id")->find();
		$where['cs_id']=$cs_id;
		$where['type_id']=1;
		$zhang_list=$this->section_obj->where($where)->order("id DESC")->select();
		$this->assign("cs_data",$cs_data);
		$this->assign("cs_id",$cs_id);
		$this->assign("zhang_list",$zhang_list);
		$this->display();
	}
    function add2(){
        $cs_id=  intval(I("get.cs_id"));
        $cs_data=$this->section_obj->where("id=$cs_id")->find();
        $where['cs_id']=$cs_id;
        $where['type_id']=1;
        $zhang_list=$this->section_obj->where($where)->order("id DESC")->select();
        $this->assign("cs_data",$cs_data);
        $this->assign("cs_id",$cs_id);
        $this->assign("zhang_list",$zhang_list);
        $this->display();
    }
    function add_zhang(){
		$cs_id=  intval(I("get.cs_id"));
		$this->assign("cs_id",$cs_id);
		$this->display();
	}
    function add_video(){

		$this->display();
	}
    function add_zhang_post(){
        if (IS_POST) {

		    $data['cs_id']=intval(I("post.cs_id"));
		    $data['type_id']=intval(I("post.type_id"));
		    $data['sc_name']=I("post.sc_name");
		    $data['addtime']=I("post.addtime");
		    $data['state']=1;
		    if($data['sc_name']==null){
		       $this->error("请填写章节名称！");
		    }
			$result=$this->section_obj->add($data);
			if ($result) {

					$this->success("添加成功！" );
				}else{
					$this->error("添加失败！");
				}
		}
	}

    function addaudio(){

		$this->display();
	}
	function add_post(){
	   if (IS_POST) {
			if ($data=$this->section_obj->create()) {
				if($data['video_type']==1){
					$data['yun_url']=I('post.yun_url');
				}else{
				    $data['yun_url']=I('post.youku_url');
				    $data['playpass']=I('post.playpass');
				}
				if ($this->section_obj->add($data)!==false) {
					session("access_url",null);
					session("access_name",null);
					$this->success("添加成功！");

				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->section_obj->getError());
			}
		}
	}
    function add_post2(){
        if (IS_POST) {
            if ($data=$this->section_obj2->create()) {
                if ($this->section_obj2->add($data)!==false) {
                    session("access_url",null);
                    session("access_name",null);
                    $this->success("添加成功！");

                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->section_obj2->getError());
            }
        }
    }
   function add_all(){

		$this->display();
	}
	public function edit(){
		$id=  intval(I("get.id"));
		$section=$this->section_obj->where("id=$id")->find();
        $section2=$this->section_obj2->where("id=$id")->find();
		$course = $this->course_obj->order(array("listorder"=>"asc"))->select();
		$this->assign("course",$course);
		$this->assign("section",$section);
        $this->assign("section2",$section2);
		$this->display();
	}
    public function edit_zhang(){
		$id=  intval(I("get.id"));
		$section=$this->section_obj->where("id=$id")->find();
		$this->assign("section",$section);
		$this->display();
	}
	public function edit_post(){
     	if (IS_POST) {
     		$id= intval(I("post.id"));
			if ($data=$this->section_obj2->create()) {
                $data['sc_text'] = htmlspecialchars_decode($data['sc_text']);
				if ($this->section_obj2->where("id=$id")->save($data)!==false) {
					$this->success("编辑成功！");
				} else {
					$this->error("编辑失败！");
				}
			} else {
				$this->error($this->section_obj2->getError());
			}
		}
	}

	//排序
	public function listorders() {
		$status = parent::_listorders($this->section_obj);
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}

	private  function _lists(){
		$count=$this->section_obj2->count();
		$page = $this->page($count, 20);
		$sectionlist=$this->sectionview_obj2->order(array('id'=>'desc'))->limit($page->firstRow . ',' . $page->listRows)->select();
		$this->assign("page", $page->show('Admin'));
		$this->assign("current_page",$page->GetCurrentPage());
		unset($_GET[C('VAR_URL_PARAMS')]);
		$this->assign("sectionlist",$sectionlist);
	}

	private function _getCourse(){

		$course = $this->course_obj->order(array("listorder"=>"asc"))->select();
		$this->assign("course",$course);
	}
	private function _getCoursename($cs_id){
	    $where['id']=$cs_id;
		$course = $this->course_obj->where($where)->select();
		$this->assign("cs_name",$course['cs_name']);
	}
	function delete(){
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			if ($this->section_obj2->delete($id)!==false) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}

		}
	}

	function check(){
		if(isset($_POST['ids']) && $_GET["check"]){

			$data["state"]=1;
			$tids=join(",",$_POST['ids']);
			if ( $this->section_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("审核成功！");
			} else {
				$this->error("审核失败！");
			}
		}
		if(isset($_POST['ids']) && $_GET["uncheck"]){

			$data["state"]=0;
			$tids=join(",",$_POST['ids']);
			if ( $this->section_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("取消审核成功！");
			} else {
				$this->error("取消审核失败！");
			}
		}
	}

     function isfree(){
		if(isset($_POST['ids']) && $_GET["free"]){

			$data["is_free"]=1;
			$tids=join(",",$_POST['ids']);
			if ( $this->section_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("设置成功！");
			} else {
				$this->error("设置失败！");
			}
		}
		if(isset($_POST['ids']) && $_GET["unfree"]){

			$data["is_free"]=0;
			$tids=join(",",$_POST['ids']);
			if ( $this->section_obj->where("id in ($tids)")->save($data)!==false) {
				$this->success("取消免费成功！");
			} else {
				$this->error("取消免费失败！");
			}
		}
	}

	function move(){
		if(IS_POST){
			if(isset($_GET['ids']) && isset($_POST['cs_id'])){
				$tids=$_GET['ids'];
				if ( $this->section_obj->where("id in ($tids)")->save($_POST)) {
					$this->success("移动成功！");
				} else {
					$this->error("移动失败！");
				}
			}
		}else{
			$course = $this->course_obj->order(array("listorder"=>"asc"))->select();
		    $this->assign("course",$course);
			$this->display();
		}
	}

	function recyclebin(){
		$this->_lists(0);
		$this->_getTree();
		$this->display();
	}

	function clean(){
		if(isset($_POST['ids'])){
			$ids = implode(",", $_POST['ids']);
			$tids= implode(",", array_keys($_POST['ids']));
			$data=array("post_status"=>"0");
			$status=$this->terms_relationship->where("tid in ($tids)")->delete();
			if($status!==false){
				$status=$this->posts_obj->where("id in ($ids)")->delete();
			}

			if ($status!==false) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}else{
			if(isset($_GET['id'])){
				$id = intval(I("get.id"));
				$tid = intval(I("get.tid"));
				$status=$this->terms_relationship->where("tid = $tid")->delete();
				if($status!==false){
					$status=$this->posts_obj->where("id=$id")->delete();
				}
				if ($status!==false) {
					$this->success("删除成功！");
				} else {
					$this->error("删除失败！");
				}
			}
		}
	}

	function restore(){
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			$data=array("tid"=>$id,"status"=>"1");
			if ($this->terms_relationship->save($data)) {
				$this->success("还原成功！");
			} else {
				$this->error("还原失败！");
			}
		}
	}

}